<?php
/**
 * @property Product $Product
 */
class ProductsController extends AppController {

    var $name = 'Products';
    var $helpers = array('Html', 'Form','Javascript' ,'Ajax');
    var $paginate=array(
        'limit'=>'20',
        'order'=>array('Product.product_name'=>'asc')
    );
    var $components = array( 'RequestHandler' );
    
    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow(array('view'));
        $this->actionMap = array(
            'admin_deletesize'=>'delete',
            'admin_editsize' => 'edit'
        );
    }

    function admin_index() {
        $this->set("products",$this->paginate());
    }


    function admin_add() {
        if (!empty($this->data)) {
            if (is_uploaded_file($this->data['Product']['image']['tmp_name'])) {
                move_uploaded_file($this->data['Product']['image']['tmp_name'],ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'img'.DS.'product'.DS.
                    $this->data['Product']['image']['name']);
                $this->data['Product']['image']=$this->data['Product']['image']['name'];
            }
            else {
                $this->data['Product']['image']="";
            }
            $this->Product->create();
            if($this->Product->save($this->data)) {
                $this->Session->setFlash("The saving is ready!");
                $this->redirect(array('action'=>'size', $this->Product->id));//ezzel ugrik vissza az előző oldalra
            }
        }
        $categories=$this->Product->Category->find("list");
        $this->set(compact("categories"));
    }

    function admin_edit($id=null) {
        if (!empty($this->data) && !empty($_POST['editproduct'])) {
            if (is_uploaded_file($this->data['Product']['image']['tmp_name'])) {
                move_uploaded_file($this->data['Product']['image']['tmp_name'],ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'img'.DS.'product'.DS.
                    $this->data['Product']['image']['name']);
                $this->data['Product']['image']=$this->data['Product']['image']['name'];
                $oldimage = $this->Product->read('image',$id);
                if(!empty($oldimage['Product']['image'])) {
                    unlink(ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'img'.DS.'product'.DS .$oldimage['Product']['image']);
                }
            }
            else {
                $this->data['Product']['image']="";
            }
        }
        if(!$id && empty($this->data)) {
            $this->Session->setFlash("Product is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        if (!empty($this->data) && !empty($_POST['editproduct']) ) {
            if($this->data['Product']['image'] == null) {
                unset($this->data['Product']['image']);
            }
            if($this->Product->save($this->data)) {
                $this->Session->setFlash("The saving is ready!");
                $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
            }
            else {
                $this->Session->setFlash("The saving not ready, plase check the data");
            }

        }
        else if(!empty($this->data) && !empty($_POST['addsize'])) {
                $this->Product->Size->create();
                $this->data['Size']['product_id'] = $id;
                if($this->Product->Size->save($this->data)) {
                    $this->Session->setFlash("Product's size saved");
                    $this->data = array();
                }
                else {
                    $this->Session->setFlash("The saving not ready, plase check the data");
                }
            }

        $this->data = $this->Product->read(null,$id);
        $this->set('product',$this->data);

        $categories=$this->Product->Category->find("list");
        $this->set(compact("categories"));
    }

    function admin_view($id=null) {
        if(!$id) {
            $this->Session->setFlash("Product is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        $this->set('product',$this->Product->read(null,$id));
    }

    function admin_delete($id=null) {
        if(!$id) {
            $this->Session->setFlash("Product is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        $image=$this->Product->read('image',$id);
        unlink(ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'img'.DS.'product'.DS .$image['Product']['image']);
        if (!$this->Product->del($id)) {
            $this->Session->setFlash("Category not exists!");
        }
        else {
            $this->Session->setFlash("Product deleted!");
        }
        $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
    }

    function view ($id=null) {
        if(!$id) {
            $this->Session->setFlash("Product is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        
        $product = $this->Product->read(null,$id);
        
        $this->set('product',$product);
        $this->set('sizes', $product['Size']);

        if(!empty($this->data)) {
            $this->Cart->addToCart($this->data['Product']['size_id'], $this->data['Product']['quantity']);
        }

        $this->data = array();
    }

    function admin_size($id=null) {
        if(empty($id)) {
            $this->Session->setFlash("Product is invalid!");
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)) {
            $this->Product->Size->create();
            $this->data['Size']['product_id'] = $id;
            if($this->Product->Size->save($this->data)) {
                $this->Session->setFlash("Product's size saved");
                $this->data = array();
            }

        }
        $product = $this->Product->read(null, $id);
        $this->set(compact('product'));
    }

    function admin_editsize($id=null) {
        if(!$id && empty($this->data) ) {
            $this->Session->setFlash("Product is invalid!");
            $this->redirect(array('action'=>'edit',$this->params['named']['product']));
        }

        if(!empty($this->data)) {
            $this->data['Product']['id']=$this->params['named']['product'];
            if($this->Product->Size->save($this->data)) {
                $this->Session->setFlash("Size saved!");
                $this->redirect(array('action'=>'edit',$this->params['named']['product']));
            }
            else {
                $this->Session->setFlash("The saving not ready, plase check the data");
            }
        }
        else {
            $this->data=$this->Product->Size->read(null,$id);
        }
    }
    function admin_deletesize($id=null) {
        if(!$id && empty($this->data) ) {
            $this->Session->setFlash("Size is invalid!");
        }
        if (!$this->Product->Size->del($id)) {
            $this->Session->setFlash("This size not exists!");
        }
        else {
            $this->Session->setFlash("The size is deleted!");
        }
        $this->redirect(array('action'=>'edit',$this->params['named']['product']));
    }

}
?>